// pages/my/my.js

import {My} from '../my/my-model.js';
import {Address} from '../../utils/address.js';
import {Order} from '../order/order-model.js';

var my = new My();
var address = new Address();
var order = new Order();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagesIndex: 1,  //订单列表的第几页
    orderArr: [],
    isLoadedAll: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._loadData();
    this._getAddressInfo();
  },

  onShow: function () {
    var orderFlag = order.hasNewOrder();
    if(orderFlag){
      this.refresh();
    }
  },

  //重新加载页面，并初始化页面参数
  refresh:function(){
    this.data.orderArr = [];
    this._getOrders(() => {
      this.data.isLoadedAll = false;
      this.data.pagesIndex = 1;
      order.execSetStorageSync(false);
    });
  },

  _getAddressInfo:function(){
    address.getAddress((addressInfo) => {
      this._bindAddressInfo(addressInfo);
    });
  },

  //地址信息数据绑定
  _bindAddressInfo(addressInfo){
    this.setData({
      addressInfo: addressInfo
    });
  },

  _loadData: function(){
    my.getUserInfo((data) => {
      this.setData({
        userInfo: data,
      })
    });

    this._getOrders();
  },

  //获得订单列表信息并添加到订单数组中
  _getOrders:function(callback){
    order.getOrders(this.data.pagesIndex, (res) => {
      var data = res.data;

      if(data.length > 0){
        this.data.orderArr.push.apply(this.data.orderArr, data)
        this.setData({
          orderArr: this.data.orderArr
        })
      }else{
        this.data.isLoadedAll = true;
      }
      callback && callback();
    })
  },

  //监听页面上拉
  onReachBottom:function(){
    if(!this.data.isLoadedAll){
      this.data.pagesIndex++;
      this._getOrders();
    }
  },

  //进入订单详情
  showOrderDetailInfo:function(event){
    var id = order.getDataSet(event, 'id');

    wx.navigateTo({
      url: '/pages/order/order?from=order&id=' + id,
    })
  },

  //付款-从我的订单列表里付款
  rePay: function (event) {
    var id = order.getDataSet(event, 'id');
    var index = order.getDataSet(event, 'index');
    this._execPay(id, index);
  },

  _execPay(id, index){
    var that = this;
    order.execPay(id, (statusCode) => {
      if(statusCode > 0){
        //支付成功
        var flag = statusCode == 2;
        if(flag){
          this.data.orderArr[index].status = 2;
          that.setData({
            orderArr: that.data.orderArr
          })
        }
        wx.navigateTo({
          url: '../pay-result/pay-result?id=' + id + '&flag=' + flag + '&from=my',
        })
      }else{
        that.showTips('支付失败', '商品已下架或库存不足');
      }
    })
  },

  //提示窗口
  showTips:function(title, content){
    wx.showModal({
      title: title,
      content: content,
    })
  },

  //编辑地址
  editAddress: function (event) {
    var that = this;

    wx.chooseAddress({
      success: function (res) {
        var addressInfo = {
          name: res.userName,
          moble: res.telNumber,
          totalDetail: address.setAddressInfo(res)
        }

        that._bindAddressInfo(addressInfo);

        //把地址保存到服务端
        address.submitAddress(res, (flag) => {
          if (!flag) {
            that.showTips('操作提示', '地址信息更新失败！');
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {
  
  // },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})